<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.Payment"%>

<script type="text/javascript"><!--
 
$(document).ready(function() {
	
	
	$("#commissionCodeTableListTable").each(function(i){
		$(this).find("th:first-child").addClass("first");
		$(this).find("td:first-child").addClass("first");
		$(this).find("tr:has(td):odd").addClass("odd");
		$(this).find("tr:has(td):even").addClass("even");
	});
	
	$("#closeEditCommissionBtn").click(function() {
		$("#editCommissionCodeTableDiv").dialog("close");
	});
	
	$("#saveEditCommissionBtn").click(function() {
		var errMsg = "";
		var array = new Array() ;
		var cid   = new Array() ;
		
		$("[id^='tr-header-']").each(function(i){
			
			var idx = this.id.substring(10);
		
			var commissionCodePk = '#commissionCodeTables\\['+idx+'\\]\\.commissionCodePk';
			if ( $(commissionCodePk).attr('checked') ) {
			var commissionCodeId = '#commissionCodeTables\\['+idx+'\\]\\.commissionCode';
			var commissionCode = $(commissionCodeId).val();			
			var amountScopeSId = '#commissionCodeTables\\['+idx+'\\]\\.amountScopeS';
			var amountScopeS = $(amountScopeSId).val();		
			var amountScopeEId = '#commissionCodeTables\\['+idx+'\\]\\.amountScopeE';
			var amountScopeE = $(amountScopeEId).val();		
			array[idx]= new commission(commissionCode,amountScopeS,amountScopeE);
		     cid.push(idx);
			}
		});
		
		
		
		for(i=0;i<cid.length;i++)
    	{
    		var first = array[cid[i]];
    		
    		var aCode=first.commissionCode;
    		var   s1 =first.amountScopeS;
    		var   e1 =first.amountScopeE;
    		//去除無超商代碼
    		  if('XXX'==aCode )
    		  {    			  
    			  continue;   			  
    		  }   		
    			    		   		
    		for(var j=0;j<cid.length;j++)
        	{
    			var next = array[cid[j]];
    			var bCode=next.commissionCode;
    			var   s2 =next.amountScopeS;
        		var   e2 =next.amountScopeE;   			
        		//去除無超商代碼
    			 if('XXX'==bCode )
    			 {      			  
       			  continue;   			  
       		  	 }
        		
    			
    			if((i != j)&& (i < j))
    			{
   			               
    				if( ((s1<s2)  && (e1>s2)) || ((s1>s2)  && (e2>s1) ) )
    				{   				
    					errMsg = errMsg +"代碼"+aCode +"與"+"代碼"+bCode+"金額區間範圍重疊<br/>"; 
    					break; 
    					
    				}                
                
    			//相同起始區間
    				if(s1==s2|| e1==e2 ||s1==e2 ||e1==s2 )
    				{
    					errMsg = errMsg +"代碼"+aCode +"與"+"代碼"+bCode+"金額區間起始範圍重複<br/>"; 
    					
    					break;
    				
    				}   			                    			  		   				
    			}    			    			  			
    			
    		}
    			
    			
        }
    	
		
	
		
		if (0 != errMsg.length){
			$("#ajaxErrorMessageDiv").html(errMsg);
			$("#ajaxErrorMessageDiv" ).dialog({  
				title:'請修正下列錯誤' 
				,width: 300
				, resizable: false
				, modal: true
				, buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});
			return false;
		}
		
		
		
		
		
		
		
		
		
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9008.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editCommissionCodeTableHiddenFrom").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {								
							if (1==$("#doAjaxSaveComissionResultCode").val()){
								$("#editCommissionCodeTableDiv").dialog("close");
							}
							$(this).dialog("close");
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				//$("#ajaxErrorMessageDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});
		
	});
	
	function commission(commissionCode,amountScopeS,amountScopeE) { 
			    this.commissionCode = commissionCode; 
	            this.amountScopeS = amountScopeS; 
		       this.amountScopeE = amountScopeE; 
			    
			   
			}; 
	
	
	
});
//
--></script>

<c:choose>
	<c:when test="${null != actionBean.commissionCodeTables}">
		<stripes:form name="editCommissionCodeTableHiddenFrom" id="editCommissionCodeTableHiddenFrom" beanclass="com.feib.soeasy.action.Soez9008ActionBean" 
            method="post" >
            <stripes:hidden name="editFormatDefinition.formatDefinitionPk"/>
            <input type="hidden" name="doAjaxSaveComission"/>
		<table id="commissionCodeTableListTable">
			<thead>
			<tr>
				<th>序號</th>
				<th>代碼</th>
				<th>代碼名稱</th>
				<th>金額<br/>區間<br/>(起)</th>
				<th>金額<br/>區間<br/>(迄)</th>
				<th>手續費<br/>類別</th>
				<th>超商<br/>成本</th>
				<th>業者<br/>負擔 </th>
				<th>繳款者<br/>負擔</th>
			</tr>
			</thead>
			<tbody>
			<c:forEach items="${actionBean.commissionCodeTables}" var="viewObj" varStatus="rowstat">
			<tr id="tr-header-${rowstat.index}">
				<td width="10%"><input name="checkCommissionCodeTables[${rowstat.index}].commissionCodePk" type="checkbox" id="commissionCodeTables[${rowstat.index}].commissionCodePk" value="<c:out value='${viewObj.commissionCodePk}'/>" <c:if test="${viewObj.checked }">checked="checked"</c:if> class="chk"/>
					${rowstat.index+1}
				</td>
				<td>
                <input type="hidden"  id="commissionCodeTables[${rowstat.index}].commissionCode" value="<c:out value='${viewObj.commissionCode}'/>" />								
				<c:out value='${viewObj.commissionCode}'/>
				</td>
				<td>				
					<c:out value='${viewObj.commissionCodeName}'/>
				</td>
				<td align="right">
				 <input type="hidden"  id="commissionCodeTables[${rowstat.index}].amountScopeS" value="<c:out value='${viewObj.amountScopeS}'/>" />				
				
					<fmt:formatNumber value="${viewObj.amountScopeS}" pattern="##,###" />
				</td>
				<td align="right">
				 <input type="hidden"  id="commissionCodeTables[${rowstat.index}].amountScopeE" value="<c:out value='${viewObj.amountScopeE}'/>" />				
				
					<fmt:formatNumber value="${viewObj.amountScopeE}" pattern="##,###" />
				</td>
				<td>
					<c:choose>
						<c:when test="${ viewObj.commissionType == 'I'}">內含</c:when>
						<c:otherwise>外加</c:otherwise>
					</c:choose>
				</td> 	
				<td align="right">
					<fmt:formatNumber value="${viewObj.commissionAmount}" pattern="##,###" />
				</td>
				<td align="right">
					<fmt:formatNumber value="${viewObj.groupAmount}" pattern="##,###" />
				</td>
				<td align="right">
					<fmt:formatNumber value="${viewObj.payerAmount}" pattern="##,###" />
				</td>			
			</tr>
			</c:forEach>
			
			<tr>
				<td colspan="9" align="center">
					<input type="button" id="saveEditCommissionBtn" value="確認"/>
					<input type="button" id="closeEditCommissionBtn" value="離開"/>
				</td>
			</tr>
			</tbody>
		</table>
		</stripes:form>
	</c:when>
	<c:otherwise>
	</c:otherwise>
</c:choose>